Regex for [a-zA-Z0-9\-] with dashes allowed in between but not at the start or end

Posted by orokusaki on Stack Overflow See other posts from Stack Overflow or by orokusaki
Published on 2010-03-26T17:27:31Z Indexed on 2010/03/26 17:33 UTC
Read the original article Hit count: 504

Filed under:
|

I'm using Python and I'm not trying to extract the value, but rather test to make sure it fits the pattern.

allowed values:

spam123-spam-eggs-eggs1
spam123-eggs123
spam
123
eggs123

I just can't have a dash at the starting or the end. There is a question on here that works in the opposite direction by getting the string value after the fact, but I simply need to test for the value so that I can disallow it. Also, it can be a maximum of 25 chars long, but a minimum of 4 chars long.

Here's what I've come up with after some experimentation with lookbehind, etc:

# Nothing here

© Stack Overflow or respective owner

Related posts about regex

Related posts about python